<?php
namespace app\admin\controller;
use think\Controller;
class Deal extends  Base
{
    private  $obj;
    public function _initialize() {
        $this->obj = model("Deal");
    }

    public function index() {
        //获取一级分类
        $categorys = model("Category")->getNormalCategoryByParentId();
        //获取二级城市
        $citys = model('City')->getNormalCitys();
    	//标题栏form按照条件查询
        $data = input('get.');
        $sdata = [];
        if(!empty($data['start_time']) && !empty($data['end_time']) && strtotime(($data['end_time'])) > strtotime($data['start_time'])){
            $sdata['create_time'] = [
                ['gt',strtotime($data['start_time'])],
                ['lt',strtotime($data['end_time'])]
            ];
        }
        if(!empty($data['category_id'])){
            $sdata['category_id'] = $data['category_id'];
        }
        if(!empty($data['city_id'])){
            $sdata['se_city_id'] = $data['city_id'];
        }



        //分类，城市 数字显示转换成中文显示
        $categoryArrs=[];
        $cityArrs=[];
        foreach ($categorys as $category){
            $categoryArrs[$category->id] = $category->name;
        }
        foreach ($citys as $city){
            $cityArrs[$city->id] = $city->name;
        }

        if(!empty($data['name'])){
            $sdata['name'] = ['like','%'.$data['name'].'%'];
        }

        $deals = $this->obj->getNormalDeals($sdata);

        return $this->fetch('', [
            'categorys' => $categorys,
        	'citys' => $citys,
            'deals' =>  $deals,
            'category_id' =>empty($data['category_id']) ? '' :$data['category_id'],
            'city_id' => empty($data['city_id']) ? '' :$data['city_id'],
            'start_time' => empty($data['start_time'])? '' :$data['start_time'],
            'end_time' =>empty( $data['end_time'])? '' :$data['end_time'],
            'name' => empty($data['name'])? '' :$data['name'],
            'categoryArrs' =>$categoryArrs,
            'cityArrs'=>$cityArrs,


        ]);
    }

    public function apply() {
        $data = input('get.');
        $sdata = [];
        if(!empty($data['start_time']) && !empty($data['end_time']) && strtotime($data['end_time']) > strtotime($data['start_time'])) {
            $sdata['create_time'] = [
                ['gt', strtotime($data['start_time'])],
                ['lt', strtotime($data['end_time'])],
            ];
        }
        if(!empty($data['category_id'])) {
            $sdata['category_id'] = $data['category_id'];
        }
        if(!empty($
            ['city_id'])) {
            $sdata['city_id'] = $data['city_id'];
        }
        if(!empty($data['name'])) {
            $sdata['name'] = ['like', '%'.$data['name'].'%'];
        }
        $cityArrs = $categoryArrs = [];
        $categorys = model("Category")->getNormalCategoryByParentId();
        foreach($categorys as $category) {
            $categoryArrs[$category->id] = $category->name;
        }

        $citys = model("City")->getNormalCitys();
        foreach($citys as $city) {
            $cityArrs[$city->id] = $city->name;
        }

        $deals = $this->obj->getApplyDeals($sdata);
        return $this->fetch('', [
            'categorys' => $categorys,
            'citys' => $citys,
            'deals' => $deals,
            'category_id' => empty($data['category_id']) ? '' : $data['category_id'],
            'city_id' => empty($data['city_id']) ? '' : $data['city_id'],
            'start_time' => empty($data['start_time']) ? '' : $data['start_time'],
            'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
            'name' => empty($data['name']) ? '' : $data['name'],
            'categoryArrs' => $categoryArrs,
            'cityArrs' => $cityArrs,
        ]);
    }

}
